﻿using System;
using System.Linq;
using System.Web.Mvc;
using sqlflashcards.Models;

namespace sqlflashcards.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index(int cardId = 1, string direction = "Next")
        {
            using (var db = new FlashcardEntities())
            {
                var maxId = db.Flashcards.OrderByDescending(u => u.sysID).FirstOrDefault().sysID;

                var minId = db.Flashcards.OrderBy(u => u.sysID).FirstOrDefault().sysID;

                if (maxId < cardId)
                {
                    cardId = maxId;
                }
                if (minId > cardId)
                {
                    cardId = minId;
                }
                Flashcard newFlashCard;
                if (direction == "Next")
                {
                    newFlashCard = (from p in db.Flashcards
                                          where p.sysID >= cardId
                                          orderby p.sysID ascending 
                                          select p).Take(1).Single();
                }
                else
                {
                    newFlashCard = (from p in db.Flashcards
                                 where p.sysID <= cardId
                                 orderby p.sysID descending 
                                 select p).Take(1).Single();
                }

                //newFlashCard.answer = newFlashCard.answer.Replace(Environment.NewLine, "<br/>");
                return View(newFlashCard);

            }
        }
        public ActionResult AddNew()
        {
            return View();
        }
        //
        // GET: /Home/
        [HttpPost]
        public ActionResult AddNew(Models.Flashcard card)
        {
			if (ModelState.IsValid)
			{
				using (var db = new FlashcardEntities())
				{
					db.AddToFlashcards(card);
					db.SaveChanges();
					return RedirectToAction("List");
				}
			}

			return View(card);

          
        }
        public ActionResult Edit(int CardId)
        {
            using (var db = new FlashcardEntities())
            {
                var product = db.Flashcards.Single(p => p.sysID == CardId);
                return View(product);
            }
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(int CardId, FormCollection collection)
        {
            using (var db = new FlashcardEntities())
            {
                var product = db.Flashcards.Single(p => p.sysID == CardId);
                TryUpdateModel(product);
                if (ModelState.IsValid)
                {
                    db.SaveChanges();
                    return RedirectToAction("List");
                }
                return View(product);
            }
        } 
        public ActionResult List()
        {
            using (var db = new FlashcardEntities())
            {
                return View(db.Flashcards.OrderByDescending(p => p.sysID).ToList());                
            }
        }

    }
}
